//# sourceURL=http://uMapApp/UI/Panel/PanelBlock/CountTableBlock
define([
    "mhjs/mhjs"
    , "./TableBlock"
    , "../Item/CountItem"
], function (
    mhjs,
    TableBlock
    , CountItem
) {

    function CountTableBlock(option) {

        var _this = this;
        _this.isOpened = false;

        _this.list = {
            "total"    : []
            , "alarm"  : []
            , "online" : []
            , "offline": []
        };

        _this.ui = _this.ui || {};

        _this.ui.countBlock = new mhjs.Div({"class": "countBlock"});

        _this.countItems = {};

        _this.countItems.totalItem = new CountItem({
            "title" : "设备数量",
            "count" : 0,
            "unit"  : "路"
            , "list": _this.list.total
        });
        _this.countItems.alarmItem = new CountItem({
            "title" : "报警数量",
            "count" : 0,
            "unit"  : "路"
            , "list": _this.list.alarm
        });
        _this.countItems.onlineItem = new CountItem({
            "title" : "在线数量",
            "count" : 0,
            "unit"  : "路"
            , "list": _this.list.online
        });
        _this.countItems.offlineItem = new CountItem({
            "title" : "离线数量",
            "count" : 0,
            "unit"  : "路"
            , "list": _this.list.offline
        });

        _this.countItems.totalItem.on("click", function (item) {
            _this.soloCountItem(item);
        });

        _this.countItems.alarmItem.on("click", function (item) {
            _this.soloCountItem(item);
        });

        _this.countItems.onlineItem.on("click", function (item) {
            _this.soloCountItem(item);
        });

        _this.countItems.offlineItem.on("click", function (item) {
            _this.soloCountItem(item);
        });

        TableBlock.call(_this, option);
        _this.addClass("countTableBlock");


        _this.ui.countBlock
            .append(_this.countItems.totalItem)
            .append(_this.countItems.alarmItem)
            .append(_this.countItems.onlineItem)
            .append(_this.countItems.offlineItem)
        ;

        _this.ui.wrap
            .before(_this.ui.countBlock.dom)
        ;


    }

    var _class = CountTableBlock;
    mhjs.Inherit(_class, TableBlock);


    Object.defineProperties(_class.prototype, {
        "app"          : {
            set: function (app) {
                var _this = this;
                _this._app = app;
                _this.countItems.totalItem.icon = _this.app.icons.layer.base64;
                _this.countItems.alarmItem.icon = _this.app.icons.alarm.base64;
                _this.countItems.onlineItem.icon = _this.app.icons.online.base64;
                _this.countItems.offlineItem.icon = _this.app.icons.offline.base64;
            },
            get: function () {
                return this._app;
            }
        },
        "data"         : {
            set: function (data) {
                var _this = this;
                _this._data = data;


                if (data.head) {

                    _this.head = data.head;

                    if (data.items) {


                        $.each(data.items, function (i, item) {

                            item.sta = item.sta || "online";
                            item.alarm = item.alarm || false;

                            _this.list.total.push(item);
                            item.tr = _this.add(item, data.format);

                            //todo new alarm list
                            if (item.alarm) {
                                _this.list.alarm.push(item);
                            }


                            if (item.sta === "offline") {
                                _this.list.offline.push(item);
                            } else {
                                _this.list.online.push(item);
                            }
                        });

                    }

                }

                _this.countItems.totalItem.count = _this.list.total.length;
                _this.countItems.alarmItem.count = _this.list.alarm.length;
                _this.countItems.offlineItem.count = _this.list.offline.length;
                _this.countItems.onlineItem.count = _this.list.online.length;


            },
            get: function () {
                return this._data;
            }
        },
        "soloCountItem": {
            value: function (activeCountItem) {

                var _this = this;

                if (activeCountItem.isActive === undefined || !activeCountItem.isActive) {

                    $.each(_this.countItems, function (i, countItem) {
                        if (countItem.isActive && countItem !== activeCountItem) {
                            countItem.active(false);
                        }
                    });

                    $.each(_this.list.total, function (i, item) {
                        item.tr.hide();
                        item.close();
                    });

                    $.each(activeCountItem.list, function (i, item) {
                        item.tr.show();
                        item.open();
                    });

                    activeCountItem.active();

                    _this.app && _this.app.flyItems(activeCountItem.list);

                } else {
                    $.each(_this.list.total, function (i, item) {
                        item.tr.show();
                        item.close();
                    });
                    activeCountItem.active(false);
                }


            }
        }
    });

    return _class;

});